package com.gluxen.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gluxen.common.constant.SystemConstant;
import com.gluxen.entity.OrderForm;
import com.gluxen.entity.User;
import com.gluxen.service.OrderService;

@Controller
public class OrderController extends BaseController{
	@Resource
	OrderService orderService;
	/**
	 * 删除订单OK
	 * @param id
	 * @param request
	 * @return
	 */
	    @RequestMapping("order_delete.json")
		@ResponseBody
		public Map<String, Object> deleteOrder(int id, HttpServletRequest request) {
			Map<String, Object> map = new HashMap<String, Object>();
			//Map<String, Object> map = new HashMap<String, Object>();
			OrderForm order = (OrderForm) orderService.queryObjectByID(OrderForm.class, id);
			try {
				    orderService.delete(order);
					map.put("success", true);
					map.put("msg", "删除成功");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("success", false);
				map.put("msg", "系统错误");
			}
			return map;
		}
	    
	    /**
	     * 批量删除订单
	     * @param order
	     * @param reques OK
	     * @return
	     */
	    @RequestMapping("order_deletes.json")
		@ResponseBody
		public Map<String, Object> deleteOrders(String[] ids, HttpServletRequest request) {
			Map<String, Object> map = new HashMap<String, Object>();
			try {
				    orderService.deleteOrder(ids);
					map.put("success", true);
					map.put("msg", "删除成功");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("success", false);
				map.put("msg", "系统错误");
			}
			return map;
		}
	    /**
	     * 修改订单
	     * @param order OK
	     * @param request
	     * @return
	     */
	    @RequestMapping("order_update.json")
		@ResponseBody
		public Map<String, Object> UpdateOrder(OrderForm order, HttpServletRequest request) {
			Map<String, Object> map = new HashMap<String, Object>();
			try {
				    orderService.updateOrder(order);
					map.put("success", true);
					map.put("msg", "修改成功");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("success", false);
				map.put("msg", "系统错误");
			}
			return map;
		}
	 
	    /**
		 * 查询所有订单信息
		 * @param OK
		 *            员工的登陆名，非必须参数
		 * @return ListVo 员工对象集合
		 */
		@RequestMapping("order_list.json")
		@ResponseBody
		public Map<String, Object> listOrder() {
			/** 所有的order信息 */
			Map<String, Object> map = new HashMap<String, Object>();
			List<OrderForm> all = new ArrayList<OrderForm>();
			try {
				all = (List<OrderForm>) orderService.queryOrder();
				map.put("orderList", all);
				map.put("success", true);
				map.put("msg", "获取订单成功");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("success", false);
				map.put("msg", "系统错误");
			}
			return map;
		}
		
		/**
		 * 按客户经理查询订单信息
		 */
		@RequestMapping("order_list_manager.json")
		@ResponseBody
		public Map<String, Object> listOrderByManager(HttpServletRequest request) {
			/** 所有的order信息 */
			Map<String, Object> map = new HashMap<String, Object>();
			User user= (User) request.getSession().getAttribute(SystemConstant.CURRENT_USER);
			List<OrderForm> all = new ArrayList<OrderForm>();
			try {
				all = (List<OrderForm>) orderService.queryOrderByManager(user.getId());
				map.put("orderList", all);
				map.put("success", true);
				map.put("msg", "获取订单成功");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("success", false);
				map.put("msg", "系统错误");
			}
			return map;
		}
		
		
		/**
		 * 按订单ID查询订单
		 * @return   OK
		 */
		@RequestMapping("order_list_id.json")
		@ResponseBody
		public OrderForm listOrderByID(int id){
			OrderForm order = null;
			try {
				order = (OrderForm) orderService.queryObjectByID(OrderForm.class, id);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return order;
		}
		
		/**
		 * 按订单的处理状态查询订单
		 * @return
		 */
		@RequestMapping("order_list_status.json")
		@ResponseBody
		public Map<String, Object> listOrderByStatus(int status){
			Map<String, Object> map = new HashMap<String, Object>();
			
			List<OrderForm> list = null;
			try {
				list=orderService.queryOrderByStatus(status);
				map.put("orderList", list);
				map.put("success", true);
				map.put("msg", "获取订单成功");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("success", false);
				map.put("msg", "系统错误");
			}
			return map;
			
		}
}
